#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cgi
import os

from bible.TheHolyBible import *
import bible.util.HolyBibleFetch

from google.appengine.api import urlfetch

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp import util

# @action /
class MainHandler(webapp.RequestHandler):
  def get(self):
    controller = BibleController()
    versions = controller.getVersions()

    template_values = {
      'versions' : versions,
      }
    template_path = os.path.join(os.path.dirname(__file__), "../template/main.html")
    self.response.out.write(template.render(template_path, template_values))

# @action /books
class BookListHandler(webapp.RequestHandler):
  def get(self):
    vcode = cgi.escape(self.request.get('vcode'))
    vname = cgi.escape(self.request.get('vname'))

    version = BibleVersion(vcode, vname)
    books = bible.util.HolyBibleFetch.getBooks(self, vcode)

    template_values = {
      'version' : version,
      'books' : books,
      }
    template_path = os.path.join(os.path.dirname(__file__), "../template/books.html")
    self.response.out.write(template.render(template_path, template_values))


# @action /chapters
class ChapterListHandler(webapp.RequestHandler):
  def get(self):

    vcode = cgi.escape(self.request.get('vcode'))
    vname = cgi.escape(self.request.get('vname'))
    bcode = (int)(cgi.escape(self.request.get('bcode')))
    bname = cgi.escape(self.request.get('bname'))

    controller = BibleController()
    version = BibleVersion(vcode, vname)
    book = BibleBook(bcode, bname)
    chapters = controller.selectChapters(bcode)

    template_values = {
      'version' : version,
      'book' : book,
      'chapters' : chapters,
      }
    template_path = os.path.join(os.path.dirname(__file__), "../template/chapters.html")
    self.response.out.write(template.render(template_path, template_values))


# @action /read
class ReadHandler(webapp.RequestHandler):
  def get(self):
    vcode = cgi.escape(self.request.get('vcode'))
    vname = cgi.escape(self.request.get('vname'))
    bcode = (int)(cgi.escape(self.request.get('bcode')))
    bname = cgi.escape(self.request.get('bname'))
    cnum = (int)(cgi.escape(self.request.get('cnum')))

    if vcode == "": vcode = "GAE"
    if bcode == "": bcode = "1"
    if cnum == "": cnum = "1"

    version = BibleVersion(vcode, vname)
    book = BibleBook(bcode, bname)
    verses = bible.util.HolyBibleFetch.getVerses(self, vcode, bcode, cnum)
    controller = BibleController()
    maxCnum = controller.selectMaxChapterNum(bcode)
    versions = controller.getVersions()

    template_values = {
      'version' : version,
      'book' : book,
      'cnum' : cnum,
      'verses' : verses,
      'maxCnum' : maxCnum,
      'versions' : versions,
      }
    template_path = os.path.join(os.path.dirname(__file__), "../template/read.html")
    self.response.out.write(template.render(template_path, template_values))


# @action /dump
class DumpHandler(webapp.RequestHandler):
  def get(self):
    fi = open("bible_chapter.list")
    i = 1
    # fi = [50]

    # fo = open('bible_gae.list', 'a')
    self.response.out.write('<pre>')
    for line in fi:
      for j in range(int(line)):
        verses = bible.util.HolyBibleFetch.getVerses(self, "GAE", i, j+1)
        k = 1
        for verse in verses:
          self.response.out.write('%s\t%s\t%s\t%s\t%s\n' % ("GAE", i, j+1, k, verse))
          # fo.write('GAE\t' + str(i) + '\t' + str(j+1) + '\t' + str(k) + '\t' + verse + '\n')
          k = k +1
      i = i +1
    self.response.out.write('<pre>')

def main():
  application = webapp.WSGIApplication([('/', MainHandler)
                                        , ('/read', ReadHandler)
                                        , ('/books', BookListHandler)
                                        , ('/chapters', ChapterListHandler)
                                        , ('/dump', DumpHandler)],
                                       debug=True)
  util.run_wsgi_app(application)


if __name__ == '__main__':
  main()
